package com.itheima.controller;
import com.itheima.common.Code;
import com.itheima.common.Result;
import com.itheima.pojo.Dish;
import com.itheima.service.DishService;
import com.itheima.vo.DishVo;
import com.itheima.vo.PagingQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.controller
 * fileName :DishController
 * creationTime :2022-11-23 13:56
 */
@RestController
@RequestMapping("dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    public Result insertDish(@RequestBody DishVo dishvo){
        Boolean insert = dishService.insertDish(dishvo);
        if (insert){
            return Result.success();
        }
        return Result.error(Code.Unauthorized.getCode());
    }

    @DeleteMapping
    public Result deleteDish(@RequestParam(name = "ids")List<String> ids){
        Boolean delete = dishService.deleteDish(ids);
        if (delete){
            return Result.success();
        }
        return Result.error(Code.NotFound.getCode());
    }

    @PutMapping
    public Result updateDish(@RequestBody DishVo dishVo){
        Boolean update=dishService.updateDish(dishVo);
        if (update){
            return Result.success();
        }
        return Result.error(Code.Unauthorized.getCode());
    }


    @PostMapping("status/{status}")
    public Result changeStatus(@PathVariable(name="status") Integer status,@RequestParam(name = "ids")List<String> ids){

        Boolean  change= dishService.changeStatus(status,ids);

        if (change){
            return Result.success();
        }
        return Result.error(Code.Unauthorized.getCode());
    }



    @GetMapping("list")
    public Result selectByCategoryId(@RequestParam(name = "categoryId")String categoryId){
        List<DishVo> dishes=dishService.selectByCategoryId(categoryId);
        return Result.success(dishes);
    }

    @GetMapping("page")
    public Result selectPage(
            @RequestParam(name = "name",required = false )String name,
            @RequestParam(name = "page" ,defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize" ,defaultValue = "2") Integer pageSize){
        PagingQueryVo<Dish> dishes=dishService.selectPage(name,page,pageSize);
        return Result.success(dishes);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable(name = "id") String id) {
        DishVo dishVo = dishService.selectById(id);
        return Result.success(dishVo);
    }

}
